package qc.module.ehs.api.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.common.core.utils.DateUtil;
import qc.module.ehs.dto.riskevaluation.HazardRiskEvaluationDto;
import qc.module.ehs.dto.riskevaluation.HazardRiskEvaluationMethodDto;
import qc.module.ehs.dto.riskevaluation.HazardRiskEvaluationQueryConditionDto;
import qc.module.ehs.service.EhsHazardRiskevlService;
import qc.module.platform.dto.uac.UacUserInfoDto;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * RiskEvaluationController
 * 危险源风险等级评价
 *
 * @author QuCheng Tech
 * @create 2024/11/5
 */
@RestController
@RequestMapping("/riskevl")
public class RiskEvaluationController {

    @Autowired
    private EhsHazardRiskevlService service;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 获取风险评价方法
     *
     * @return List<HazardRiskEvaluationMethodDto>
     * @author QuCheng Tech
     * @since 2024/11/5
     */
    @GetMapping("/methods")
    public List<HazardRiskEvaluationMethodDto> getRiskEvaluationMethods() {
        return service.getRiskEvaluationMethods();
    }

    /**
     * 评价指定危险源风险
     *
     * @param dto 请求参数
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/11/5
     * @description 保存指定危险源的风险评价结果;保存评价记录后需要对危险源信息表中的风险等级进行更新，对单元中的危险源数量统计进行更新；
     */
    @RequestMapping(value = "/evaluation",method = {RequestMethod.GET,RequestMethod.POST})
    public String evaluationRisk(@RequestBody HazardRiskEvaluationDto dto) throws QCPromptException {
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        //获取用户信息
        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);

        return service.evaluationRisk(dto, userInfo);
    }

    /**
     * 获取指定危险源最新评价记录
     * 
     * @param id 危险源ID
     * @return HazardRiskEvaluationDto
     * @author QuCheng Tech
     * @since 2024/11/8
     */
    @GetMapping("/latest")
    public HazardRiskEvaluationDto latestByParam(@RequestParam String id){
        return service.latest(id);
    }

    /**
     * 获取指定危险源最新评价记录
     *
     * @param id 危险源ID
     * @return HazardRiskEvaluationDto
     * @author QuCheng Tech
     * @since 2024/11/8
     */
    @GetMapping("/latest/{id}")
    public HazardRiskEvaluationDto latestByPath(@PathVariable String id) {
        return service.latest(id);
    }

    /**
     * 查询指定单元的危险源风险评价记录
     *
     * @param condition 查询条件
     * @return java.util.List<qc.module.ehs.dto.riskevaluation.HazardRiskEvaluationDto>
     * @author QuCheng Tech
     * @since 2025/6/11
     */
    @RequestMapping(value = "/unit", method = {RequestMethod.GET, RequestMethod.POST})
    public List<HazardRiskEvaluationDto> queryByUnit(@RequestBody HazardRiskEvaluationQueryConditionDto condition) throws QCPromptException {
        if (condition == null)
            throw new QCPromptException("查询条件不能为空");
        if (StringUtils.isBlank(condition.getUnitid()))
            throw new QCPromptException("单元ID必须指定");
        if (StringUtils.isBlank(condition.getBegintm()))
            throw new QCPromptException("查询起始时间不能为空");
        if (StringUtils.isBlank(condition.getEndtm()))
            throw new QCPromptException("查询截止时间不能为空");
        //查询时间处理
        Date beginDate = null;
        Date endDate = null;
        if (StringUtils.isNotBlank(condition.getBegintm())) {
            try {
                beginDate = DateUtil.parseDate(condition.getBegintm());
            } catch (Exception e) {
                // 时间转换错误，抛出自定义异常
                throw new QCPromptException("查询条件中的开始日期转换错误");
            }
        }
        if (StringUtils.isNotBlank(condition.getEndtm())) {
            try {
                endDate = DateUtil.parseDate(condition.getEndtm());
            } catch (Exception e) {
                // 时间转换错误，抛出自定义异常
                throw new QCPromptException("查询条件中的截止日期转换错误");
            }
        }
        if (beginDate == null)
            throw new QCPromptException("查询起始时间无效");
        if (endDate == null)
            throw new QCPromptException("查询截止时间无效");
        if (beginDate.after(endDate))
            throw new QCPromptException("查询起始时间不能大于截止时间");

        return service.queryByUnit(condition.getUnitid(), beginDate, endDate);
    }
}
